package org.rambadger.command.axon;


import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;
import org.mongojack.JacksonDBCollection;

import java.lang.reflect.Type;

/**
 * Jersey Provider to inject {@link JacksonDBCollection} instances into jersey resources/providers that are annotated with {@link JCollection @JCollection}
 *
 * @see MongoBundle
 * @see JCollection
 */
public class CollectionProvider implements InjectableProvider<JCollection, Type> {
    private final MongoBundle mongo;

    /**
     * Instantiates a new Collection provider with the {@link MongoBundle}.
     *
     * @param mongo the mongo bundle
     */
    public CollectionProvider(MongoBundle mongo) {
        this.mongo = mongo;
    }

    @Override
    public ComponentScope getScope() {
        return ComponentScope.Singleton;
    }

    @Override
    public Injectable<JacksonDBCollection> getInjectable(ComponentContext ic, final JCollection annotation, Type type) {
        final JacksonDBCollection collection = mongo.getWrappedCollection(annotation.type(), annotation.key());
        return new  Injectable<JacksonDBCollection>() {
            @Override
            public JacksonDBCollection getValue() {
                return collection;
            }
        };
    }
}